//
//  ControllerPlusToolbar.m
//  ChimooTimer
//
//  Created by Ryan Poling on 5/6/2008.
//  Copyright 2008 Chimoosoft. All rights reserved.
//

#import "ControllerPlusToolbar.h"
#import "StopWatch.h"

@implementation Controller (ControllerPlusToolbar)

#pragma mark -
#pragma mark Toolbar methods

// sets the action, picture, and label for each toolbar item
- (NSToolbarItem *)toolbar:(NSToolbar *)toolbar 
	 itemForItemIdentifier:(NSString*)itemIdentifier 
 willBeInsertedIntoToolbar:(BOOL)flag {
	
	NSToolbarItem * item;
	item = [[NSToolbarItem alloc] initWithItemIdentifier:itemIdentifier];
	
	
	if ([itemIdentifier isEqualToString:@"HControl"]) {
		NSSize controlSize;
		controlSize.width = [hView frame].size.width;
		controlSize.height = [hView frame].size.height;
		[item setLabel:@"Hours"];
		[item setPaletteLabel:@"Hours"];
		[item setMinSize:controlSize];
		[item setMaxSize:controlSize];
		[item setView:hView];
	} else if ([itemIdentifier isEqualToString:@"MControl"]) {
		NSSize controlSize;
		controlSize.width = [mView frame].size.width;
		controlSize.height = [mView frame].size.height;
		[item setLabel:@"Minutes"];
		[item setPaletteLabel:@"Minutes"];
		[item setMinSize:controlSize];
		[item setMaxSize:controlSize];
		[item setView:mView];
	} else if ([itemIdentifier isEqualToString:@"SControl"]) {
		NSSize controlSize;
		controlSize.width = [sView frame].size.width;
		controlSize.height = [sView frame].size.height;
		[item setLabel:@"Seconds"];
		[item setPaletteLabel:@"Seconds"];
		[item setMinSize:controlSize];
		[item setMaxSize:controlSize];
		[item setView:sView];
	} else if ([itemIdentifier isEqualToString:@"UpDownControl"]) {
		NSSize controlSize;
		controlSize.width = [upDownView frame].size.width;
		controlSize.height = [upDownView frame].size.height;		
		[item setMinSize:controlSize];
		[item setMaxSize:controlSize];
		[item setView:upDownView];
		[item setLabel:@"Direction"];
		[item setPaletteLabel:@"Direction"];
		[item setToolTip:NSLocalizedString(@"Toggle Direction", nil)];
	} else if ([itemIdentifier isEqualToString:@"ToggleDirectionButton"]) {
		[item setLabel:@"Direction"];
		[item setPaletteLabel:@"Direction"];
		[item setToolTip:NSLocalizedString(@"Toggle Direction", nil)];
		[item setAction:@selector(toggleDirection:)];
		[item setImage:[NSImage imageNamed:@"Down"]];		
	} else if ([itemIdentifier isEqualToString:@"StartStopButton"]) {
		[item setLabel:NSLocalizedString(@"Start/Stop", nil)];
		[item setPaletteLabel:NSLocalizedString(@"Start/Stop", nil)];
		[item setToolTip:NSLocalizedString(@"Start/Stop Timer", nil)];
		[item setAction:@selector(startStop:)];
		[item setImage:[NSImage imageNamed:@"Start"]];
	} else if ([itemIdentifier isEqualToString:@"PreferencesButton"]) {
		[item setLabel:NSLocalizedString(@"Preferences", nil)];
		[item setPaletteLabel:NSLocalizedString(@"Preferences", nil)];
		[item setToolTip:NSLocalizedString(@"Open Preferences", nil)];
		[item setAction:@selector(showPreferencePanel:)];
		[item setImage:[NSImage imageNamed:@"PreferencesButton"]];		
	} else if ([itemIdentifier isEqualToString:@"ResetButton"]) {
		[item setLabel:NSLocalizedString(@"Reset", @"")];
		[item setPaletteLabel:NSLocalizedString(@"Reset", @"")];	
		[item setImage:[NSImage imageNamed:@"Reset"]];
		[item setAction:@selector(reset:)];
	} else if ([itemIdentifier isEqualToString:@"LogButton"]) {
		[item setLabel:NSLocalizedString(@"Log", @"")];
		[item setPaletteLabel:NSLocalizedString(@"Log", @"")];	
		[item setImage:[NSImage imageNamed:@"Log"]];
		[item setAction:@selector(logTime:)];
	}
	
	[item autorelease];	
	return item;
}


// returns an array of all allowable toolbar item identifiers
- (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar *)toolbar {
	
	// **************
	// add option to add 30 seconds or some other interval like the microwave
	///////////////
	
	NSArray * array = nil;
	
	if (toolbar == multiplierToolbar) {
		array = [NSArray arrayWithObjects:
				 NSToolbarSeparatorItemIdentifier,
				 NSToolbarSpaceItemIdentifier,
				 NSToolbarFlexibleSpaceItemIdentifier,
				 NSToolbarCustomizeToolbarItemIdentifier,
				 @"ToggleDirectionButton",
				 @"StartStopButton",
				 @"PreferencesButton",
				 @"LogButton",
				 @"ResetButton", nil];
	} else if (toolbar == mainToolbar) {
		array = [NSArray arrayWithObjects:
				 NSToolbarSeparatorItemIdentifier,
				 NSToolbarSpaceItemIdentifier,
				 NSToolbarFlexibleSpaceItemIdentifier,
				 NSToolbarCustomizeToolbarItemIdentifier,
				 @"HControl", 
				 @"MControl",
				 @"SControl",
				 @"UpDownControl",
				 @"ToggleDirectionButton",
				 @"StartStopButton",
				 @"PreferencesButton",
				 @"LogButton",
				 @"ResetButton", nil];		 
	}
	
	return array;
}

// Returns an array representing the default set of toolbar identifiers.
- (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar *)toolbar {
	NSArray * array = nil;
	
	if (toolbar == multiplierToolbar) {
		array = [NSArray arrayWithObjects:
				 @"PreferencesButton",
				 NSToolbarFlexibleSpaceItemIdentifier,
				 @"StartStopButton",
				 @"ResetButton", nil];		
		
	} else if (toolbar == mainToolbar) {
		array = [NSArray arrayWithObjects:
				 @"HControl",
				 @"MControl",
				 @"SControl",
				 NSToolbarFlexibleSpaceItemIdentifier,
				 @"UpDownControl",
				 @"StartStopButton",
				 @"ResetButton", nil];		
	}
	
	return array;
}

- (BOOL)validateToolbarItem:(NSToolbarItem *)theItem {
	if ([[theItem itemIdentifier] isEqualToString:@"StartStopButton"]) {
		if ([stopWatch isRunning]) [theItem setImage:[NSImage imageNamed:@"Stop"]];
		else [theItem setImage:[NSImage imageNamed:@"Start"]];
	} else if ([[theItem itemIdentifier] isEqualToString:@"ToggleDirectionButton"]) {
		if ([stopWatch isInCountDownMode]) {
			[theItem setImage:[NSImage imageNamed:@"Up"]];
			[theItem setToolTip:@"Count up."];
		} else {
			[theItem setImage:[NSImage imageNamed:@"Down"]];
			[theItem setToolTip:@"Count down."];
		}
	}
	
	return YES;
}

@end
